<template>
  <div>
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive && isCellPhone"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive && isCellPhone"></router-view>
    <div class="not-cellphone" v-if="!isCellPhone">
       请使用移动设备访问！
    </div>
  </div>
</template>

<script>
console.log(process.env.NODE_ENV);
export default {
    name: "app",
    data(){
        return{
        isCellPhone: false
        }
    },
    created() {
        let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
        if(flag){
            this.isCellPhone = true;
        }else{
            this.isCellPhone = false;
        }
        if(this.getUrlParams().id){
            this.$store.commit('updateId', this.getUrlParams().id);
        }
        // this.queryOpenId('031TD72w3SrgDW2hSv0w3jIonv4TD726');
        if(!this.$store.state.openId && !this.getUrlParams().code){
            location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23b7dd374176dfe8&redirect_uri=http%3A%2F%2Fwx.gongxiaomei.cn&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
        }else if(this.getUrlParams().code && !this.$store.state.openId){
            this.queryOpenId(this.getUrlParams().code);
        }
    },
    mounted(){
        // updateOpenId
    },
    methods:{
        queryToken(){
            '031TD72w3SrgDW2hSv0w3jIonv4TD726'
        },
        queryOpenId(code){
            this.$axios.get(this.$axios.baseUrl + "/wechat/authorize?code=" + code).then(res => {
                console.log(res)
                this.$store.commit('updateOpenId', res.data.openId);
                this.$store.commit('weixinUserInfo', res.data);
            }, (err) => {
                console.log(err)
            });
        },
        getUrlParams() {
            let url = window.location.search;
            let index = url.indexOf('?')
            let obj = {}
            if (index !== -1) {
                let str = url.substr(1)
                let arr = str.split('&')
                for (let i = 0; i < arr.length; i++) {
                    obj[arr[i].split('=')[0]] = arr[i].split('=')[1]
                }
            }
            return obj
        },
    }
};
</script>
<style scoped>
  .not-cellphone{
    font-size: 18px;
    margin: 15px 0;
    text-align: center;
  }
</style>



